package com.cliave.ttweb.service.impl;

import com.cliave.ttweb.bean.Dishes;
import com.cliave.ttweb.exception.GlobalException;
import com.cliave.ttweb.mapper.DishMapper;
import com.cliave.ttweb.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Date;

/**
 * @Service和@Controller是一个意思 都是spring的ioc 注释
 * 这个时候对象已经被new出来了  放到spring容器中了
 * 我们现在学习的是5.0版本
 * 3.0版本的spring   ioc注释是这么写的
 * @Component
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Override
    public boolean addDishes(Dishes dishes) {
        Date date = new Date();
        dishes.setBuyCount(0);
        dishes.setStatus(1);
        dishes.setCreated(date);
        dishes.setUpdated(date);
        int i = dishMapper.addDishes(dishes);
        if (i<=0){
            return false;
        }
        return true;
    }

    @Override
    public Dishes findDishById(Integer id) {
        Dishes dishes = dishMapper.findDishById(id);
        return dishes;
    }

    @Override
    public boolean updateDish(@Valid Dishes dishes) {
        if (dishes.getCategory().getId()==null){
            throw new GlobalException("菜品分类不能为空");
        }
        Date date = new Date();
        dishes.setUpdated(date);
        int i = dishMapper.updateDish(dishes);
        if (i<=0){
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteDish(Integer id) {
       int i = dishMapper.deleteDish(id);
        if (i<=0){
            return false;
        }
        return true;
    }
}
